<%--
  Created by IntelliJ IDEA.
  User: 86181
  Date: 2022/11/12
  Time: 1:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登记模特框</title>
</head>
<body>
        <form id="warehousingform">
        <div class="modal-header">
            <input type="submit" class="btn btn-default" value="提交">
            <button class="close" data-dismiss="modal">退出</button>
        </div>
        <div class="modal-body">
            <div class="row">
                <div class="col-md-offset-5">
                    <h2><strong>入库登记</strong></h2>
                </div>
                <input type="hidden" id="warehousingmoalid" name="id">
            </div>
            <br>
            <div class="row">
            <div class="col-md-6">
                <table>
                    <tr>
                        <td>入库单编号:</td>
                        <td><input type="text" id="warehousinggatherId" name="gatherId"  readonly style="outline: none;border: none"></td>
                    </tr>
                    <tr></tr>
                    <tr>
                        <td>入库理由:</td>
                        <td><input type="text" id="warehousingreasonexact" readonly style="outline: none;border: none"></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table>
                    <tr>
                        <td>入库详情理由:</td>
                        <td><input type="text" id="warehousingreason" readonly style="outline: none;border: none"></td>
                    </tr>
                </table>
            </div>
        </div>
            <br><br>
            <div style="height:45%">
                <table class="table" id="warehouingmodaltab">

                </table>
            </div>
            <br>
            <div class="row">
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>应入库总件数:</td>
                            <td><input type="text" id="warehousingamountSum" readonly style="outline: none;border: none" ></td>
                        </tr>
                        <tr>
                            <td>已入库总件数:</td>
                            <td><input type="text" id="warehousinggatheredAmountSum" name="gatheredAmountSum" readonly style="outline: none;border: none" ></td>
                        </tr>

                        <tr>
                            <td>应入库总成本:</td>
                            <td><input type="text" id="warehousingcostPriceSum" readonly style="outline:none;border:none;"></td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>应入库总成本:</td>
                            <td><input type="text" id="warehousingcostPriceSumss"  readonly style="outline:none;border:none;"></td>
                        </tr>
                        <tr>
                            <td>登记人</td>
                            <td><input type="text" name="register" id="warehousingregister" readonly style="outline: none;border: none;border-bottom:1px black dashed"></td>
                        </tr>

                        <tr>
                            <td><strong>登记时间</strong></td>
                            <td><input type="text" readonly name="registerTime" id="warehousingregisterTime" style="outline: none;border: none"></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        </form>

        <script>
            //加载数据
            $(function () {

                $("#warehouingmodaltab").bootstrapTable({
                    contentType: "application/x-www-form-urlencoded", //如果用tomcat就要写这个
                    striped: true,//是否显示隔行变色
                    cache: false,//是否缓存
                    pagination: false,//是否显示分页
                    sortable: false,//是否显示排序
                    clickToSelect: true,//是否启用点击选中行
                    uniqueId:"id",//每一行的唯一标识，一般为主键列,
                    showRefresh: true,//是否显示刷新按钮
                    columns:[
                        {
                            field:"",
                            title:"序号",
                            formatter:function (value,row,index) {
                                return Number(index)+1
                            }
                        },
                        {
                            field:"productId",
                            title:"产品编号"
                        },
                        {
                            field:"productName",
                            title:"产品名称",
                        },
                        {
                            field:"costPrice",
                            title:"单价",
                        },
                        {
                            field:"amount",
                            title:"应入库件数"
                        },
                        {
                            field:"warehousing",
                            title:"已入库数量"
                        },
                        {
                            field:"gatheredAmount",
                            title:"本次入库数量",
                            formatter:function (value,row,index) {
                               if(Number(row.warehousing)!=Number(row.amount)){
                                   return "<input type='number' class='gatherdamountss' style='width:80px' value='"+value+"' required onchange='numwarehousing(this)'>"
                               }else {
                                   return "完成"
                               }
                            }
                        }
                    ]
                })
            })
            //加载入库表  入库详情表数据
            $(function () {

                var datetimestack;
                //点击获取当前时间
                $.get("queryCellByDate.action",function (data) {
                    datetimestack=data.datetime;
                },"json")
                //获取值
                var gatherId='${param.gatherId}'
                $.post("queryByGatherGatherdetailswarhousing.action",{"gatherId":gatherId},function (data) {
                    //赋值
                    //入库单编号
                    $("#warehousinggatherId").val(data.gatherId);
                    //入库理由
                    $("#warehousingreasonexact").val(data.reasonexact)
                    //入库详情理由
                    $("#warehousingreason").val(data.reason)
                    //应入库总件数
                    $("#warehousingamountSum").val(data.amountSum)
                    //入库总成本
                    $("#warehousingcostPriceSum").val(data.costPriceSum)
                    //登记人
                    $("#warehousingregister").val(data.register)
                    //登记时间
                    $("#warehousingregisterTime").val(datetimestack)
                    //已入库数量
                    $("#warehousinggatheredAmountSum").val(data.gatheredAmountSum)
                    //id
                    $("#warehousingmoalid").val(data.id)
                    //入库明细表数据
                    var gatherDetailss=data.gatherDetails;
                    var arr=[];
                    for(var i=0;i<gatherDetailss.length;i++){
                        arr.push(gatherDetailss[i])
                    }
                    //添加到表格中
                   $("#warehouingmodaltab").bootstrapTable("append",arr);
                   var arr1=$("#warehouingmodaltab").bootstrapTable("getData");
                    var num=0;
                    $(arr1).each(function (index,item) {
                        num+=Number(item.warehousing);
                    })
                    var nums=0;
                    $(arr1).each(function (index,item) {
                        nums+=Number(item.warehousing)*Number(item.costPrice);
                    })
                    $("#warehousingcostPriceSumss").val(nums)
                    //给已入库数量赋值
                    $("#warehousinggatheredAmountSum").val(num)

                    $("#warehouingmodaltab").bootstrapTable("load",arr1);
                },"json")
            })
          /*  //点击提交做修改状态
            function warehousingudatecheck() {
                //修改 入库表 和 入库明细表状态
                var arr=$("#warehousingform").serializeArray();
                //创建数组
                var gather={}
                $(arr).each(function (index,item) {
                    gather[item.name]=item.value;
                })
                //获取表格的数据
                var arr1=$("#warehouingmodaltab").bootstrapTable("getData");
                gather["gatherDetails"]=arr1;

                //赋值给控制层
                $.ajax({
                    url:"updateWarehosingGaher.action",
                    data:JSON.stringify(gather),
                    contentType: "application/json",
                    dataType:"json",
                    type:"post",
                    success:function (data) {
                        alert(data.msg)
                        //关闭模态框
                        $("#warehousingmodal").modal("hide")
                        //刷新表格
                        $("#Warehousingtab").bootstrapTable("refresh")
                    }
                })
                //获取form的值
            }*/
            function numwarehousing(num) {
                var nums=$(num).val();
                var warehousing=$(num).parent().parent().find("td").eq(5);
                var nums1=$(num).parent().parent().find("td").eq(4).text();
                var arr1=$("#warehouingmodaltab").bootstrapTable("getData");
                var productIds=$(num).parent().parent().find("td").eq(1).text();
                $(arr1).each(function (index,item) {
                    if(item.productId==productIds){
                        item.gatheredAmount=nums;
                    }

                })
                $("#warehouingmodaltab").bootstrapTable("load",arr1)
            }
            $("#warehousingform").validate({
                submitHandler:function () {
                    var numi=0;
                    //验证数字
                    var gatherdamountss=$(".gatherdamountss")
                    var num;
                    var num1;
                    var vales
                    $(gatherdamountss).each(function (index,item) {
                       num=$(item).parent().parent().find("td").eq(4).text();
                       num1=$(item).parent().parent().find("td").eq(5).text();
                       vales=item.value
                        if( Number(num1)+Number(vales)>Number(num)){
                            numi++;
                        }
                    })
                    if(numi==0){
                        //修改 入库表 和 入库明细表状态
                        var arr=$("#warehousingform").serializeArray();
                        //创建数组
                        var gather={}
                        $(arr).each(function (index,item) {
                            gather[item.name]=item.value;

                        })
                        //获取表格的数据
                        var arr1=$("#warehouingmodaltab").bootstrapTable("getData");
                        gather["gatherDetails"]=arr1;

                        //赋值给控制层
                        $.ajax({
                            url:"updateWarehosingGaher.action",
                            data:JSON.stringify(gather),
                            contentType: "application/json",
                            dataType:"json",
                            type:"post",
                            success:function (data) {
                                alert(data.msg)
                                //关闭模态框
                                $("#warehousingmodal").modal("hide")
                                //刷新表格
                                $("#Warehousingtab").bootstrapTable("refresh")
                            }
                        })
                    }else {
                        alert("请输入正确的数字")
                    }

                }
            })
        </script>
</body>
</html>
